package com.application.race.manage.player;

import com.application.race.manage.DEF;

	public class SideClipContrl
	{

		public Player actorContrl;
  
		public  SideClipContrl(Player actorContrl)
		{
			this.actorContrl=actorContrl;
		}
		public double resistanceCft;
		public double speed;
        public double sideClipAngle=0;

		public double alpha=1;
		public double frictLostRate=0.8;
		public double F1,F2,F,temp,sideSpeed;
		double angleF1,oriAngle,theta;
		double F1s2;
  		double F2s2;
  		double F1F2x2;
  		static int i0=0;
  		static int i360=360;
  		static int i180=180;
  		static int i1=1;
  		static int i2=2;
  		static int i15=15;
  		static int i24=24;
  		static int i90=90;
  		static int i_1=-1;
  		double number;
        public void setSideClipData(double eft,double spd,int angle){
        	hasSideClip=true;
            frictLostRate=actorContrl.carInfo.slipResistance*eft;
            if(frictLostRate>=i1){
            	hasSideClip=false;
            	return;
            }
            if(spd<=i0)return;
            if(actorContrl._down){
            	angle+=i180;
            	angle%=i360;
            		actorContrl.down_speed=frictLostRate*spd;
            }else if(actorContrl._up){
            		actorContrl.up_speed=frictLostRate*spd;
            }
	        sideSpeed=(i1-frictLostRate)*spd;
	        alpha=actorContrl._accCft;
	         
            if(speed==i0){ 
	            speed=sideSpeed;
	            sideClipAngle=angle;
	            
            }else{ 
	           if(sideClipAngle>angle){
	      		    theta=sideClipAngle-angle;

	      		    if(theta>i180){
	      		    	theta=i360-theta;
                		F1=sideSpeed;
                		F2=speed;
                		oriAngle=sideClipAngle;
	      		    } else {
	          		    F1=speed;
                		F2=sideSpeed;
                		oriAngle=angle;
	      		    }
	           }else{
	           	    theta=angle-sideClipAngle;
	      		    if(theta>i180){
	      		    	theta=i360-theta;
	      		    	F1=speed;
                		F2=sideSpeed;
                		oriAngle=angle;
	      		    } else{
                		F1=sideSpeed;
                		F2=speed;
                		oriAngle=sideClipAngle;
	      		    }
	           }
 
      		    if(theta==i0){
      		    	speed=speed+sideSpeed;
      		    	return;
      		    }
      		    if(theta==i180){
      		    	if(speed>sideSpeed){
      		    		speed=speed-sideSpeed;
      		    		return;
      		    	}else{
      		    		speed=sideSpeed-speed;
      		    		sideClipAngle=angle;
      		    		return;
      		    	}
      		    }
    		    F1s2=F1*F1;
    		    F2s2=F2*F2;
    		    F1F2x2=F1*F2*i2;
    		    temp=F1F2x2*Math.cos(number=Math.PI-angleToRadiou(theta));
                F=Math.sqrt(number=F1s2+F2s2-temp);
                speed=F;
	            temp=(F*F+F2s2-F1s2)/(i2*F*F2);
	            if(temp>i1){
	              	temp=i1;
	            }
	            if(temp<i_1){
	            	temp=i_1;
	            }
	           // trace("b  "+temp );
	            temp=Math.acos(temp);
	            angleF1=radiouToAngle(temp);
//	            trace("angle  "+temp+"  " +angleF1+"  "+sideClipAngle+"  "+F+"  "+F1+"  "+F2+"  "+theta);
	            sideClipAngle=oriAngle+angleF1;
	            sideClipAngle=sideClipAngle%i360;
 
            }
        }
        double ret0;
        double angleToRadiou(double angle){
        	    ret0=angle/i360*Math.PI*i2;
        	   return ret0;
        }
        double ret1;
        double radiouToAngle(double radiou){
        	  ret1=(int)(radiou/(Math.PI*i2)*i360);
        	   return ret1;
        } 
        public double tx,ty;
        public double avgSpd=0;
        public boolean hasSideClip=false;
        public boolean nextSide(double time){
       	    avgSpd=sideClip(time);
       	    if(avgSpd<=i0)hasSideClip=false;
            //if(avgLost<=0)actorContrl.in_side_clip=false;
            if(actorContrl._down){
                temp=Math.floor(number=(actorContrl._angle+i180)/i15)%i24;
            }else{
            	temp=Math.floor(number=actorContrl._angle/i15)%i24;
            }
            //if(avgLost<=0)actorContrl.in_side_clip=false;
 
			actorContrl._vx=DEF.sinTable[(int)temp]*(actorContrl._speed);
			actorContrl._vy=DEF.cosTable[(int)temp]*(actorContrl._speed)*i_1;
			tx=Math.sin(number=angleToRadiou(sideClipAngle))*avgSpd;
			ty=Math.cos(number=angleToRadiou(sideClipAngle))*avgSpd*i_1;
			actorContrl._vx=actorContrl._vx+tx;
        	actorContrl._vy=actorContrl._vy+ty; 
            return true;
         
        }
        double  startTime,nowTime=0;
        double t0,t1,t2,V=0,V0,V1,a,startV,endV;
	    double sideClip(double time){
		    V=speed;
		    V1=i0;
		    if(V>V1){
		    	a= alpha;
		    	startTime=V/a;
		    	nowTime=startTime-time;
		    	t0=	startTime;
		    	t2= nowTime;
				startV=t0*a;
				endV=t2*a;
				if(endV<i0){
					t1=i0;
					endV=i0;
					V0=(startV+endV)/i2;
					V=V0*(t0-t1)/(t0-t2);

				}else{
					V=(startV+endV)/i2;
				}
			    speed=endV;
		    }else{
	    	    speed=i0;
		    	return V1;
		    }
		    return V;	
		}
    	double spd;
    	double ang;
        public void addCrash(double vx,double vy){
        	spd=Math.sqrt(number=vx*vx+vy*vy);	
        	ang=radiouToAngle(number=Math.atan2(vy*i_1,vx));
            if(ang<=i0){
             	  ang=i90-ang;
             }else{
             	  ang=i90-ang;
             	  ang=(ang+i360)%i360;
             }

            hasSideClip=true;
            alpha=actorContrl._accCft;
            actorContrl._speed=i0;
            actorContrl.up_speed=i0;
            actorContrl.down_speed=i0;
            this.sideClipAngle=ang;
            this.speed=spd;
            // if(reset)
             //actorContrl.resetAction();
        }


	}

